var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var config = require('../../config');
var util = require('../../utils/util.js')
var app = getApp();

Page({
  data: {
    loginUrl: config.service.loginUrl,
    createRoom: config.service.createRoom,
    userInfo: {},
    room_name: '',
    room_id: '',
    chip: '',
    isLoading: false
  },
  bindKeyInput1: function(e) {
    this.setData({
      room_name: e.detail.value
    })
  },
  bindKeyInput2: function(e) {
    this.setData({
      chip: e.detail.value
    })
  },
  createRoom: function() {
    var self = this;
    self.setData({
      isLoading: true
    });
    qcloud.clearSession();
    qcloud.request({
      url: self.data.createRoom,
      data: {
         room_name: self.data.room_name,
         chip: self.data.chip
      },
      // 请求之前是否登陆，如果该项指定为 true，会在请求之前进行登录
      login: true,
      success(resp) {
          console.log('request success');
          self.setData({
            isLoading: false
          });
          if(resp.data.code === 0){
              self.setData({
                room_id: resp.data.data.room_id
              });
              wx.navigateTo({
                url: '../details/details?room_id='+ resp.data.data.room_id + '&room_name=' + resp.data.data.room_name + '&chip=' +self.data.chip
              });
          }else{
              util.showSuccess(resp.data.message);
          }
      },
      fail(error) {
          console.log('request fail');
      }
    });
  },
  onLoad: function () {
    var self = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      self.setData({
        userInfo:userInfo
      })
    })
  }
})
